package com.work.mybatis.mapper;

import com.work.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.boot.autoconfigure.security.SecurityProperties;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-05-22
 * Time:18:45
 */
@Mapper//spring能识别到
public interface UserInfoMapper {
    //查询
    @ResultMap("resultMap")
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();
    @Select("select * from userinfo where delete_flag=#{deleteFlag}")
    List<UserInfo> getUserInfoByDelete(Integer deleteFlag);

    @Select("select * from userinfo where delete_flag=#{deleteFlag} and gender=#{gender}")
    List<UserInfo> getUserInfo2(Integer deleteFlag,Integer gender);


    //增
    //实现--前面是对应的字段，后面是赋的值--从对象中拿值
    @Options(useGeneratedKeys = true,keyProperty = "id")//获取自增id
    @Insert("insert into userinfo(username,password,age,gender) " +
            "values(#{username},#{password},#{age},#{gender})")
    Integer insert(UserInfo userInfo);//对象的定义

    //用Param实现
    @Insert("insert into userinfo(username,password,age,gender) " +
            "values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);//对象的定义

    //删除
    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    //更新
    @Update("update userinfo set password = #{password}, age = #{age}, gender = #{gender} where id = #{id}")
    Integer update(UserInfo userInfo);

    //对应不上时的方式
    //1.起别名
    @Select("SELECT id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime," +
            " update_time as updateTime FROM userinfo")
    List<UserInfo> selectUserInfos();

    /*指定结果映射关系*/
    @Results(id ="resultMap",value = {//指定id
            @Result(column = "delete_flag",property = "deleteFlag"),//第一个是列名，第二个是属性名
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("SELECT * FROM userinfo")
    List<UserInfo> selectUserInfos2();

    /*驼峰自动转换*/
    @Select("SELECT * FROM userinfo")
    List<UserInfo> selectUserInfos3();

    /*SQL注入*/
    @Select("select * from userinfo where username = #{username}")
    UserInfo selectUserByName(String username);

    @Select("select * from userinfo where username = '${username}'")
    UserInfo selectUserByName2(String username);

    //排序
    @Select("select * from userinfo order by id ${order}")
    List<UserInfo> queryUserListByOrder(String order);

    //like查询
    @Select("select * from userinfo where username like CONCAT('%',#{username},'%')")
    List<UserInfo> queryUserListByLike(String username);

}
